package com.handydata.antifraud.ws.dap;

import java.util.Date;

import javax.annotation.PostConstruct;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.handydata.antifraud.annotation.Comment;
import com.handydata.antifraud.common.constant.DataSourceCode;
import com.handydata.antifraud.common.exception.WsInvokeException;
import com.handydata.antifraud.to.dap.JingZongThreeFactorsReqTO;
import com.handydata.antifraud.to.dap.JingZongThreeFactorsResTO;
import com.handydata.antifraud.util.DateUtil;
import com.handydata.antifraud.util.encrypt.Md5Utils;
import com.handydata.antifraud.ws.WsInvocationService;

/**
 * 敬众三要素验证
 *
 */
@Slf4j
@Service("jingZongThreeFactorsWsService")
@Comment("敬众三要素验证")
public class JingZongThreeFactorsWsServiceImpl extends
		DataPlatformWsService<JingZongThreeFactorsReqTO, JingZongThreeFactorsResTO>
		implements
		WsInvocationService<JingZongThreeFactorsReqTO, JingZongThreeFactorsResTO> {

	@Override
	public JingZongThreeFactorsResTO query(JingZongThreeFactorsReqTO req)
			throws WsInvokeException {
		log.info("敬众三要素验证请求参数 {}", req);
		JingZongThreeFactorsResTO resTO = new JingZongThreeFactorsResTO();
		if (StringUtils.isEmpty(req.getName())) {
			log.error("敬众三要素验证参数校验 {}", "name为空");
			resTO.setResCode("1111");
			resTO.setResMsg("敬众三要素验证接口name参数为空");
			return resTO;
		}
		if (StringUtils.isEmpty(req.getIdCard())) {
			log.error("敬众三要素验证参数校验 {}", "idCard为空");
			resTO.setResCode("1111");
			resTO.setResMsg("敬众三要素验证接口idCard参数为空");
			return resTO;
		}
		if (StringUtils.isEmpty(req.getPhoneNum())) {
			log.error("敬众三要素验证参数校验 {}", "phone为空");
			resTO.setResCode("1111");
			resTO.setResMsg("敬众三要素验证接口phone参数为空");
			return resTO;
		}
		this.dataType = DataSourceCode.JING_ZONG_THREE_FACTORS.getCode();
		resTO = executeQuery(req);
		return resTO;
	}

	@Override
	protected String createQueryXml(JingZongThreeFactorsReqTO req) {
		StringBuilder xml = new StringBuilder();
		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
		xml.append("<subatm>");
		xml.append("<application>GwBiz.Req</application>");
		xml.append("<version>1.0.0</version>");
		xml.append("<sendTime>").append(DateUtil.now14()).append("</sendTime>");
		xml.append("<transCode>100091</transCode>");
		xml.append("<channelId>").append(encryInfo.getChannelId()).append("</channelId>");
		xml.append("<channelOrderId>").append(DateUtil.now17()).append("</channelOrderId>");
		xml.append("<phone>").append(Md5Utils.md5ToHexStr(req.getPhoneNum())).append("</phone>");
		xml.append("<name>").append(req.getName()).append("</name>");
		xml.append("<idCard>").append(Md5Utils.md5ToHexStr(req.getIdCard())).append("</idCard>");
		xml.append("</subatm>");
		log.info("敬众三要素调用翰迪数据平台请求报文：{}", xml);
		req.setQueryXml(xml.toString());
		return xml.toString();
	}

	@Override
	protected JingZongThreeFactorsResTO parseResultAndSave(String resultXml,	String orderId, Date createDate) {
		log.info("调用敬众三要素查询接口返回报文：{}", resultXml);
		JingZongThreeFactorsResTO resTO = new JingZongThreeFactorsResTO();
		try {
			// 解析xml，保存本地DB
			Document doc = DocumentHelper.parseText(resultXml);
			Element rootElt = doc.getRootElement(); // 获取根节点
			// String phone = rootElt.elementTextTrim("phone"); //
			// 拿到subatm节点下的子节点phone值
			// String name = rootElt.elementTextTrim("name"); //
			// 拿到subatm节点下的子节点name值
			// String idCard = rootElt.elementTextTrim("idCard"); //
			// 拿到subatm节点下的子节点idCard值
			String respCode = rootElt.elementTextTrim("respCode"); // 拿到subatm节点下的子节点respCode值
			String respDesc = rootElt.elementTextTrim("respDesc"); // 拿到subatm节点下的子节点respDesc值
			resTO.setRespCodeOfJingZongThreeFactors(respCode);
			resTO.setResCode(respCode);
			resTO.setResMsg(respDesc);
		} catch (Exception e) {
			log.info("调用敬众三要素查询接口返回报文解析异常：{}", e);
			resTO.setResCode("1111");
			resTO.setResMsg("调用敬众三要素查询接口返回报文解析异常");
		}
		log.info("调用敬众三要素查询接口返回结果：{}", JSONObject.toJSONString(resTO));
		return resTO;
	}

	@SuppressWarnings("unchecked")
	@PostConstruct
	@Override
	public void register() {
		registerToWsServiceFactory(this, JingZongThreeFactorsReqTO.class);
	}

}
